package core;

import fileoperation.FileDeletion;
import fileoperation.FileStatus;
import repository.Repository;
import stage.Index;

import java.io.File;
import java.io.IOException;

public class JitRM {
    /**
     * jit rm FileName(default mode)
     * remove file from index and work space (consider file changes)
     * @param fileName
     * @throws IOException
     */
    public static void rmDefault(String fileName) throws IOException {

        File rmFileObject = new File(Repository.getWorkTree() + File.separator + fileName);
        Index index = Index.getInstance();

        if(!rmFileObject.exists()){ // File does not exist
            System.out.println("fatal: pathspec '"+ fileName +"' did not match any files");
            return;
        }

        boolean hasChange = FileStatus.fileChanged(Repository.getWorkTree(), fileName, index.getIndexFileList());

        if (rmFileObject.isFile()) {
            if (index.getFileIndex(rmFileObject) == -1){ // File index does not exist
                System.out.println("fatal: pathspec '" + fileName + "' did not match any files");
            }
            else if(!hasChange){ // The file has not changed
                index.removeIndex(rmFileObject);
                FileDeletion.deleteFile(Repository.getWorkTree() + File.separator + fileName);
            }else{
                System.out.println("error: the following file has changes staged in the index:\n" + fileName +
                        "\n(use --cached to keep the file, or -f to force removal)\n");
            }

        }
        else if(rmFileObject.isDirectory()){

            File rmDirectoryObject = new File(Repository.getWorkTree() + fileName);
            File[] fileObjects = rmDirectoryObject.listFiles();

            for(int i = 0; i < fileObjects.length; i++)
                rmDefault(fileObjects[i].getPath()); // delete files from index one by one
        }
    }


    /**
     * jit rm --cached FileName
     * remove file from index
     * @param fileName
     * @throws IOException
     */
    public static void rmCached(String fileName) throws IOException {

        File rmFileObject = new File(Repository.getWorkTree() + File.separator + fileName);

        if(!rmFileObject.exists()){// File does not exist
            System.out.println("fatal: pathspec '"+ fileName +"' did not match any files");
            return;
        }

        Index index = Index.getInstance();
        if (rmFileObject.isFile()) {
            if (index.getFileIndex(rmFileObject) != -1){
                index.removeIndex(rmFileObject);
            }
            else // File index does not exist
                System.out.println("fatal: pathspec '" + fileName + "' did not match any files");
        }
        else if(rmFileObject.isDirectory()){

            File rmDirectoryObject = new File(Repository.getWorkTree() + fileName);
            File[] fileObjects = rmDirectoryObject.listFiles();

            for(int i = 0; i < fileObjects.length; i++)
                rmCached(fileObjects[i].getPath()); // delete files from index one by one
        }
    }


    /**
     * jit rm -f FileName
     * remove file from index and work space(ignore file changes)
     * @param fileName
     * @throws IOException
     */
    public static void rmF(String fileName) throws IOException {

        File rmFileObject = new File(Repository.getWorkTree() + File.separator + fileName);
        if(!rmFileObject.exists()){ // File does not exist
            System.out.println("fatal: pathspec '"+ fileName +"' did not match any files");
            return;
        }

        Index index = Index.getInstance();
        if (rmFileObject.isFile()) {
            if (index.getFileIndex(rmFileObject) != -1){
                index.removeIndex(rmFileObject);
                FileDeletion.deleteFile(Repository.getWorkTree() + File.separator + fileName);
            }
            else // File index does not exist
                System.out.println("fatal: pathspec '" + fileName + "' did not match any files");
        }
        else if(rmFileObject.isDirectory()){
            File rmDirectoryObject = new File(Repository.getWorkTree() + fileName);
            File[] fileObjects = rmDirectoryObject.listFiles();
            for(int i = 0; i < fileObjects.length; i++)
                rmF(fileObjects[i].getPath()); // delete files from index one by one
        }

    }


}
